<?php

namespace App\Controller\Api\User;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Model\OrderSubscribe;
use App\Model\SubBanner;
use App\Model\SubOption;
use App\Model\User;
use App\Service\AppleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use App\Middleware\JWTAuthMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;

/**
 * @Middleware(JWTAuthMiddleware::class)
 * @Controller(prefix="/user/v1/subscribe")
 */
class SubscribeController extends BaseController
{

    /**
     * @Inject()
     * @var AppleService
     */
    protected $appleService;


    /**
     * 订阅列表
     * @RequestMapping(path="subscribeList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function subscribeList()
    {

        $lang = getLang();

        $cacheKey = 'subscribeList:' . $lang;


        $subscribeList = cache()->get($cacheKey);

        if (is_null($subscribeList)) {
            $subscribeList = SubOption::where(['status' => 1, 'lang' => getLang()])->orderBy('sort', 'desc')->get()->toArray();

            if (!empty($subscribeList)) {
                cache()->set($cacheKey, $subscribeList);
            }
        }

        return $this->response->returnSuccess(['list' => $subscribeList]);

    }


    /**
     * 订阅背景banner列表
     * 缓存--subscribeBannerList
     * @RequestMapping(path="bannerList")
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    public function bannerList()
    {

        $bannerList = cache()->get('subscribeBannerList');
        if (is_null($bannerList)) {
            $bannerList = SubBanner::where(['status' => 1])->orderBy('sort', 'desc')->get()->toArray();
            if (!empty($bannerList)) {
                cache()->set('subscribeBannerList', $bannerList);
            }
        }


        return $this->response->returnSuccess(['list' => $bannerList]);

    }


    /**
     * 生成订阅订单
     * @RequestMapping(path="createSubscribeOrder")
     */
    public function createSubscribeOrder()
    {

        //判断是否已经订阅
        $role = User::role($this->uid());
        if ($role) {
            return $this->response->returnFail(ErrorCode::ALREADY_SUBSCRIBE);
        }
        $code = $this->request->post('code','');

        $orderNo = OrderSubscribe::createSubscribeOrder($this->uid(),$code);

        if ($orderNo){
            return $this->response->returnSuccess(['orderNo'=>$orderNo]);
        }else{
            return  $this->response->returnFail();
        }

    }


    /**
     *
     * 订阅订单回调
     * @RequestMapping(path="confirmOrder")
     * @return ResponseInterface
     */
    public function confirmOrder(){

        $params = $this->request->post();

        $this->validate($params, [
            'orderNo' => 'required',
            'payId' => 'required',
        ]);

        $check = $this->appleService->checkSubscribeBill($params['orderNo'],$params['payId']);
        if ($check){
            $rel = OrderSubscribe::completeOrder($params['orderNo'],$params['payId']);
            if ($rel){
                $orderInfo = OrderSubscribe::where(['order_no'=>$params['orderNo']])->first()->toArray();

                return $this->response->returnSuccess(['payStatus'=>true,'orderInfo'=>$orderInfo]);
            }

        }

        return $this->response->returnSuccess(['payStatus'=>false,'orderInfo'=>null]);

    }

    /**
     * @RequestMapping(path="subscribeRestore")
     *
     */
    public function subscribeRestore(){

        $payId = $this->request->post('payId');
        $role = User::where(['id'=>$this->uid()])->value('role');
        if ($role == 1){
            //已经是会员了
            return $this->response->returnSuccess();
        }
        $checkStatus = $this->appleService->subscribeRestore($this->uid(),$payId);
        if ($checkStatus == true){
            return $this->response->returnSuccess();
        }else{
            return $this->response->returnFail();
        }
    }


}
